<template>
  <div>
    <!-- 头部返回区域 -->
    <div class="header">
      <router-link to="/profile" tag="div" class="iconfont back_icon">&#xe616;</router-link>
      <h3>注册</h3>
    </div>
    <!-- 注册表单区域 -->
    <div class="main">
      <el-form ref="registerFormRef" :model="registerForm" :rules="registerFormRules" label-width="3.2rem" :label-position="'left'">
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入您的用户名"></el-input>
        </el-form-item>
        <!-- 手机号 -->
        <el-form-item label="手机号码" prop="user_tel">
          <el-input v-model="registerForm.user_tel" placeholder="请输入您的手机号"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="registerForm.password" placeholder="请输入您的密码"></el-input>
        </el-form-item>
        <!-- 确认密码 -->
        <el-form-item label="确认密码" prop="checkPassword">
          <el-input type="password" v-model="registerForm.checkPassword" placeholder="请再次输入您的密码"></el-input>
        </el-form-item>
      </el-form>
      <el-form>
        <!-- 提交按钮 -->
        <el-form-item>
          <el-button class="btn" @click="register">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import qs from 'qs'
export default {
  name: 'Register',
  data () {
    // 验证手机号码规则
    var checkPhone = (rule, value, callback) => {
      const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
      if (reg.test(value)) {
        callback()
      } else {
        return callback(new Error('请输入正确的手机号'))
      }
    }
    // 验证两次密码是否输入一致
    var checkPass = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      // 注册表单的数据绑定对象
      registerForm: {
        username: '',
        user_tel: '',
        password: '',
        checkPassword: ''
      },
      // 表单的验证规则对象
      registerFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        // 验证手机号是否合法
        user_tel: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { validator: checkPhone, trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        // 验证确认密码是否合法
        checkPassword: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          { validator: checkPass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async register () {
      this.$refs.registerFormRef.validate(async valid => {
        if (valid) {
          const form = {
            username: this.registerForm.username,
            user_tel: this.registerForm.user_tel,
            password: this.registerForm.password
          }
          const loginForm = {
            tel: this.registerForm.user_tel,
            password: this.registerForm.password
          }
          // Qs.stringify()将对象序列化成URL的形式，Qs是axios里面自带的
          const formStr = qs.stringify(form)
          const loginFormStr = qs.stringify(loginForm)
          // 注册用户
          const { data: res } = await this.$http.post('/api/user/register', formStr, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
          if (res.meta.status !== 200) {
            return this.$message.error(res.meta.msg)
          } else {
            // 用户登录
            const { data: res1 } = await this.$http.post('/api/user/login', loginFormStr, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
            if (res1.meta.status !== 200) return this.$message.error(res1.meta.msg)
            // 将返回的信息保存在sessionStorage中
            window.sessionStorage.setItem('user', JSON.stringify({ user_id: res1.data.user_id, username: res1.data.username, user_sex: res1.data.user_sex, user_tel: res1.data.user_tel }))
            // 保存登录信息
            this.$store.commit('login')
            // 跳转到个人中心页面
            this.$router.push('/profile')
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
// 头部区域
.header {
  position: relative;
  width: 100%;
  height: 1.6rem;
  line-height: 1.6rem;
  font-size: .72rem;
  text-align: center;
  background: #fff;
  // 返回按钮
  .back_icon {
    position: absolute;
    left: .4rem;
    top: 50%;
    transform: translateY(-50%);
    font-size: .8rem;
  }
}
// 注册表单区域
.main {
  padding: .4rem;
}
.el-button {
  width: 100%;
  font-size: 0.56rem!important;
  color: #fff!important;
  border: 0!important;
  background: #999!important;
}
</style>
